/**
 * 显示消息提示框
 * @param content 提示的标题
 */
export function toast(content) {
	uni.showToast({
		icon: 'none',
		title: content
	})
}

/**
 * 显示模态弹窗
 * @param content 提示的标题
 */
export function showConfirm(content) {
	return new Promise((resolve, reject) => {
		uni.showModal({
			title: '提示',
			content: content,
			cancelText: '取消',
			confirmText: '确定',
			success: function(res) {
				resolve(res)
			}
		})
	})
}

/**
 * 参数处理
 * @param params 参数
 */
export function tansParams(params) {
	let result = ''
	for (const propName of Object.keys(params)) {
		const value = params[propName]
		var part = encodeURIComponent(propName) + "="
		if (value !== null && value !== "" && typeof(value) !== "undefined") {
			if (typeof value === 'object') {
				for (const key of Object.keys(value)) {
					if (value[key] !== null && value[key] !== "" && typeof(value[key]) !== 'undefined') {
						let params = propName + '[' + key + ']'
						var subPart = encodeURIComponent(params) + "="
						result += subPart + encodeURIComponent(value[key]) + "&"
					}
				}
			} else {
				result += part + encodeURIComponent(value) + "&"
			}
		}
	}
	return result
}



/**
 * wxlogin
 * @param  获取code
 */
export function getWxCode(data) {
	return new Promise((resolve, reject) => {
		uni.login({
			provider: "weixin",
			scopes: "auth_base",
			success(res) {
				resolve(res)
			},
			fail(err) {
				reject(err)
			}
		})
	})
}


/**
 * 对象转 URL 查询字符串
 * @param {Object} params 参数对象
 * @param {Boolean} encode 是否进行 encodeURIComponent 编码
 * @returns {String} "key=value&key2=value2"
 */
export function toQuery(params = {}, encode = true) {
	if (!params || typeof params !== 'object') return ''
	const result = []

	const build = (key, value) => {
		if (value === null || value === '' || typeof value === 'undefined') return
		if (typeof value === 'object' && !Array.isArray(value)) {
			for (const subKey in value) {
				build(`${key}[${subKey}]`, value[subKey])
			}
		} else if (Array.isArray(value)) {
			value.forEach((v, i) => build(`${key}[${i}]`, v))
		} else {
			const part = encode ? `${encodeURIComponent(key)}=${encodeURIComponent(value)}` : `${key}=${value}`
			result.push(part)
		}
	}

	for (const key in params) {
		build(key, params[key])
	}

	return result.join('&')
}

/**
 * URL 查询字符串转对象
 * @param {String} queryStr "a=1&b=2"
 * @returns {Object}
 */
export function toObject(queryStr = '') {
	if (!queryStr) return {}
	const search = queryStr.startsWith('?') ? queryStr.substring(1) : queryStr
	const pairs = search.split('&')
	const obj = {}
	pairs.forEach(pair => {
		const [key, val] = pair.split('=')
		if (!key) return
		obj[decodeURIComponent(key)] = decodeURIComponent(val || '')
	})
	return obj
}

/**
 * 获取指定 URL 中的查询参数对象
 * @param {String} url 完整链接
 * @returns {Object}
 */
export function parseUrlParams(url = '') {
	try {
		const query = url.split('?')[1] || ''
		return toObject(query)
	} catch (e) {
		return {}
	}
}

/**
 * 将参数对象拼接到指定 URL
 * @param {String} url
 * @param {Object} params
 * @returns {String} 拼接后的完整 URL
 */
export function appendParams(url, params = {}) {
	if (!params || Object.keys(params).length === 0) return url
	const query = this.toQuery(params)
	return url.includes('?') ? `${url}&${query}` : `${url}?${query}`
}